using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Util;

namespace Moogle
{
    /// <summary>
    /// Pop/Remind/Edit Activity Class.  This activity is used to modify pop reminder settings attached to a given medication.  It's target consumer is 
    /// the ActivityPopRemind class.
    /// </summary>
    [Activity(Label = "Pop Reminders > Edit", MainLauncher = false, Icon = "@drawable/icon", NoHistory = true, Theme = "@android:style/Theme.Holo.Light")]
    [IntentFilter(new[] { Intent.ActionMain })]
    public class ActivityPopRemindEdit : Activity
    {
        #region Local Memory
        
        /// <summary>
        /// Constant using to identify (filter) entries created in the
        /// Android Log by this class.
        /// </summary>
        private const string __C_TAG = "ActivityPopRemindEdit";
        private Button __M_buttonBack = null;
        private Button __M_buttonUpdate = null;
        private TextView __M_textViewTop = null;
        private dbCommands __M_clsDbCommands = null;
        private ListView __M_listViewPopTimes = null;
        private List<PopTime> __M_listAllPopTimes = null;
        private String __M_stringMedicationName = string.Empty;
        private String __M_stringTimeStamps = string.Empty;
        private List<PopTime> __M_listSelectedPopTimes = new List<PopTime>();
        private Boolean __M_booleanNoData = false;
        private PopTimeListAdapter __M_popTimeListAdapter = null;

        #endregion
        #region OnCreate Control Function
        
        /// <summary>
        /// Class (or Activity) entry point.
        /// </summary>
        /// <param name="bundle">Android.OS.Bundle object instance that can be used to pass data to activity at startup.</param>
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            Log.Info(__C_TAG, "OnCreate started.");
            try
            {
                /// Load Layout
                SetContentView(Resource.Layout.remindpopedit);

                /// Store the input passed 
                this.__M_stringMedicationName = Intent.GetStringExtra("Medication") ?? "Unknown";

                Log.Info(__C_TAG, "this.__M_stringMedicationName=" + this.__M_stringMedicationName);

                /// Configure the back (aka cancel) button
                #region ...
                this.__M_buttonBack = (Button)this.FindViewById(Resource.Id.doBack);
                this.__M_buttonBack.Click += delegate
                {
                    var _IntentShow = new Intent(this, typeof(ActivityRemind));
                    StartActivity(_IntentShow);
                    
                }; // end delegate
                #endregion

                /// Configure the update button
                #region ...
                this.__M_buttonUpdate = (Button)this.FindViewById(Resource.Id.doUpdate);
                this.__M_buttonUpdate.Click += delegate
                {
                    if(null!= this.__M_popTimeListAdapter)
                    {
                        /// Delete the current time entries from the PopTime Table
                        this.__M_clsDbCommands.DeleteMedicationPopTimes(this.__M_stringMedicationName);

                        foreach (PopTime _pop in this.__M_popTimeListAdapter.PopTimes)
                        {
                            Log.Info(__C_TAG, "Updating PopTime table ...");
                            var _rc = this.__M_clsDbCommands.AddPopTime(_pop.MedicationName, _pop.Hour, _pop.Minute, _pop.AnteMeridiem, _pop.PostMeridiem);
                            Log.Info(__C_TAG, "rc=" + _rc.ToString());
                        } // end foreach
                    } // end if

                    var _IntentShow = new Intent(this, typeof(ActivityRemind));
                    StartActivity(_IntentShow);

                }; // end delegate
                #endregion

                /// Get All PopTimes from the database
                this.__M_clsDbCommands = new dbCommands(this);
                this.__M_listAllPopTimes = (List<PopTime>)this.__M_clsDbCommands.GetAllPopTimes();


                if ( this.__M_listAllPopTimes.Count > 0 && this.__M_stringMedicationName != "Unknown")
                {
                    Log.Info(__C_TAG, "this.__M_listAllPopTimes.Count =" + this.__M_listAllPopTimes.Count);
                    /// Populate the selected Medication Pop Time List
                    foreach (PopTime _pop in this.__M_listAllPopTimes)
                    {
                        if (_pop.MedicationName.Trim().ToUpper() == this.__M_stringMedicationName.Trim().ToUpper())
                        {
                            this.__M_listSelectedPopTimes.Add(_pop);
                        } // end if
                    } // end foreach

                    if (this.__M_listSelectedPopTimes.Count > 0)
                    {

                        /// Configure the TextView Control
                        #region ...
                        __M_textViewTop = FindViewById<TextView>(Resource.Id.TextViewEditPopMedication);
                        __M_textViewTop.Text = this.__M_stringMedicationName;
                        #endregion

                        /// Configure the List View
                        #region ...
                        this.__M_listViewPopTimes = FindViewById<ListView>(Resource.Id.listViewEditPopTime);

                        Log.Debug(__C_TAG, "Configuring PopTimeListAdapter...");
                        this.__M_popTimeListAdapter = new PopTimeListAdapter(this, (List<PopTime>)this.__M_listSelectedPopTimes);
                        this.__M_listViewPopTimes.Adapter = this.__M_popTimeListAdapter;
                       
                        #endregion
                    } // end if
                    else
                        this.__M_booleanNoData = true;
                } // end if
                else
                    this.__M_booleanNoData = true;
               
                /// If the no data flag is false, then
                /// disable the no data message.
                if (!this.__M_booleanNoData)
                {
                    this.__M_textViewTop.Text = this.__M_stringMedicationName;
                }

            } // end try
            catch (SystemException err)
            {
                Log.Error(__C_TAG, err.Message);
            } // end catch
            finally
            {
                Log.Info(__C_TAG, "OnCreate ended.");
            } // end finally

        }
        #endregion
    }
}